package springcloud.fallback;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

/**
 * @Author 咕唧
 * @Date 2022/6/28 11:57
 * @Version 1.0
 */
@Component
public class MyZuulFallback implements FallbackProvider {
    private ObjectMapper objectMapper;

    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    /**
     * 当前的降级方法是给哪一个服务使用 *和null都代表所有服务
     *
     * @return
     */
    @Override
    public String getRoute() {
        return null;
    }

    /**
     * 当前方法的返回值就是降级数据，
     *
     * @param route 当前服务的下游服务是谁
     * @param cause 发生的问题
     * @return
     */
    @Override
    public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
        System.out.println("MyZuulFallback的fallbackResponse方法执行了");

        return new ClientHttpResponse() {
            /**
             * 设置返回的状态
             * @return
             * @throws IOException
             */
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return HttpStatus.OK;
            }

            /**
             * 返回状态对应的状态码
             * @return
             * @throws IOException
             */
            @Override
            public int getRawStatusCode() throws IOException {
                return 200;
            }

            /**
             * 返回状态嘛的提示信息
             * @return
             * @throws IOException
             */
            @Override
            public String getStatusText() throws IOException {
                return "成功";
            }

            @Override
            public void close() {

            }

            /**
             * 返回的响应正文
             * @return
             * @throws IOException
             */
            @Override
            public InputStream getBody() throws IOException {
                HashMap<Object, Object> hashMap = new HashMap<>();
                hashMap.put("code", 0);
                hashMap.put("msg", "fail");
                String s = objectMapper.writeValueAsString(hashMap);
                return new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
            }

            /**
             * 返回的响应头
             * @return
             */
            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("content-Type", "application/json;charset=utf-8");
                return httpHeaders;
            }
        };
    }
}
